package org.lex.utils.text;

import java.text.DateFormat;
import java.util.Date;

/**
 * ��ʽ��ʱ��Ĺ�����
 */
public class DateTimeUtilities {
	public static final int FULL = DateFormat.FULL;
	public static final int LONG = DateFormat.LONG;
	public static final int MEDIUM = DateFormat.MEDIUM;
	public static final int SHORT = DateFormat.SHORT;
	public static final int DEFAULT = DateFormat.DEFAULT;

	// ========================================
	private static DateFormat[][] dateTimeFormatCache = null;
	private static DateFormat[] timeFormatCache = null;
	private static DateFormat[] dateFormatCache = null;
	static {
		dateTimeFormatCache = new DateFormat[4][4];
		timeFormatCache = new DateFormat[4];
		dateFormatCache = new DateFormat[4];
	}

	private DateTimeUtilities() {
	}

	public static String date() {
		return formatDateTime(System.currentTimeMillis());
	}

	public static void println() {
		System.out.println(formatDateTime(System.currentTimeMillis()));
	}

	public static String filename(long time) {
		StringBuilder buffer = new StringBuilder();
		buffer.append(DateTimeUtilities.formatDateTime(time, DateFormat.MEDIUM, DateFormat.MEDIUM));
		for (int i = 0; i < buffer.length(); i++) {
			char ch = buffer.charAt(i);
			if (true == Character.isWhitespace(ch))
				buffer.setCharAt(i, '.');
			if (':' == ch)
				buffer.setCharAt(i, '-');
		}
		return buffer.toString();
	}

	/*
	 * DateTime Format
	 */
	public static String formatDateTime(long time) {
		return formatDateTime(time, DateFormat.DEFAULT, DateFormat.DEFAULT);
	}

	public static String formatDateTime(long time, int style) {
		return formatDateTime(time, style, style);
	}

	public static String formatDateTime(long time, int dateStyle, int timeStyle) {
		return getDateTimeFormat(dateStyle, timeStyle).format(new Date(time));
	}

	public static DateFormat getDateTimeFormat(int dateStyle, int timeStyle) {
		int row = getStyleIndex(dateStyle);
		int column = getStyleIndex(timeStyle);
		DateFormat format = dateTimeFormatCache[row][column];
		if (null == format) {
			format = DateFormat.getDateTimeInstance(dateStyle, timeStyle);
			dateTimeFormatCache[row][column] = format;
		}
		return format;
	}

	/*
	 * Time Format
	 */
	public static String formatTime(long time) {
		return getTimeFormat(DateFormat.DEFAULT).format(new Date(time));
	}

	public static String formatTime(long time, int timeStyle) {
		return getTimeFormat(timeStyle).format(new Date(time));
	}

	public static DateFormat getTimeFormat(int timeStyle) {
		int index = getStyleIndex(timeStyle);
		DateFormat format = timeFormatCache[index];
		if (null == format) {
			format = DateFormat.getTimeInstance(timeStyle);
			timeFormatCache[index] = format;
		}
		return format;
	}

	/*
	 * Date Format
	 */
	public static String formatDate(long time) {
		return getDateFormat(DateFormat.DEFAULT).format(new Date(time));
	}

	public static String formatDate(long time, int timeStyle) {
		return getDateFormat(timeStyle).format(new Date(time));
	}

	public static DateFormat getDateFormat(int dateStyle) {
		int index = getStyleIndex(dateStyle);
		DateFormat format = dateFormatCache[index];
		if (null == format) {
			format = DateFormat.getDateInstance(dateStyle);
			dateFormatCache[index] = format;
		}
		return format;
	}

	private static int getStyleIndex(int style) {
		switch (style) {
		case DateFormat.FULL:
			return 0;
		case DateFormat.LONG:
			return 1;
		case DateFormat.MEDIUM:
			return 2;
		case DateFormat.SHORT:
			return 3;
		default:
			return DateFormat.DEFAULT;
		}
	}
}
